/******************************************************************************
 * The MIT License
 *
 * Copyright (c) 2010, LeafLabs, LLC.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *****************************************************************************/

 /*
 * 2021 Jan 28: Edited by JasonZhu (Crobot)
 */
#ifndef _SERVO_H_
#define _SERVO_H_

#include "stdint.h"
#include "stddef.h"

#define NOT_ATTACHED                        (-1)
#define ATTACHED                            (0)

#define INIT_ERROR                          (-1)
#define INIT_SUCCESS                        (0)


// Default min/max pulse widths (in microseconds) and angles (in
// degrees).  Values chosen for Arduino compatibility.  These values
// are part of the public API; DO NOT CHANGE THEM.
#define MIN_ANGLE               0
#define MAX_ANGLE             180

#define MIN_PULSE_WIDTH       544     // the shortest pulse sent to a servo
#define MAX_PULSE_WIDTH      2400     // the longest pulse sent to a servo

typedef void (*SERVO_Init_Func)(void);
typedef void (*SERVO_WritePulseWidth_Func)(uint16_t);

typedef struct 
{
  SERVO_Init_Func                   Init;
  SERVO_WritePulseWidth_Func       WritePulseWidth;
}SERVO_IO_t;

typedef struct 
{
  SERVO_IO_t                              IO;     
  uint8_t                           minAngle;
  uint8_t                           maxAngle;   
  uint8_t                          zeroAngle;  
}SERVO_Object_t;

void SERVO_SetZero(SERVO_Object_t *pObj, uint8_t zero);
int32_t SERVO_AttachIO(SERVO_Object_t *pObj, SERVO_IO_t *pIO);
int32_t SERVO_Init(SERVO_Object_t *pObj, uint8_t initAngle, uint8_t minAngle, uint8_t maxAngle);
void SERVO_WriteAngle(SERVO_Object_t *pObj, uint8_t target_angle);
void SERVO_ChangeAngle(SERVO_Object_t *pObj, int16_t target_angle);
#endif  /* _SERVO_H_ */
